package com.td.module.farmer.service.employ;

import com.td.framework.common.pojo.PageResult;
import com.td.module.farmer.controller.admin.employ.vo.EmployCreateReqVO;
import com.td.module.farmer.controller.admin.employ.vo.EmployExportReqVO;
import com.td.module.farmer.controller.admin.employ.vo.EmployPageReqVO;
import com.td.module.farmer.controller.admin.employ.vo.EmployUpdateReqVO;
import com.td.module.farmer.convert.employ.EmployConvert;
import com.td.module.farmer.dal.dataobject.employ.EmployDO;
import com.td.module.farmer.dal.mysql.employ.EmployMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.farmer.enums.ErrorCodeConstants.EMPLOY_NOT_EXISTS;

/**
 * 农业社会化服务 Service 实现类
 *
 * @author 王翔翔
 */
@Service
@Validated
public class EmployServiceImpl implements EmployService {

    @Resource
    private EmployMapper employMapper;

    @Override
    public Long createEmploy(EmployCreateReqVO createReqVO) {
        // 插入
        EmployDO employ = EmployConvert.INSTANCE.convert(createReqVO);
        employMapper.insert(employ);
        // 返回
        return employ.getId();
    }

    @Override
    public void updateEmploy(EmployUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEmployExists(updateReqVO.getId());
        // 更新
        EmployDO updateObj = EmployConvert.INSTANCE.convert(updateReqVO);
        employMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmploy(Long id) {
        // 校验存在
        this.validateEmployExists(id);
        // 删除
        employMapper.deleteById(id);
    }

    private void validateEmployExists(Long id) {
        if (employMapper.selectById(id) == null) {
            throw exception(EMPLOY_NOT_EXISTS);
        }
    }

    @Override
    public EmployDO getEmploy(Long id) {
        return employMapper.selectById(id);
    }

    @Override
    public List<EmployDO> getEmployList(Collection<Long> ids) {
        return employMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<EmployDO> getEmployPage(EmployPageReqVO pageReqVO) {
        return employMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EmployDO> getEmployList(EmployExportReqVO exportReqVO) {
        return employMapper.selectList(exportReqVO);
    }

}
